package com.xhdp.config;

import com.xhdp.handle.LoginInterceptor;
import com.xhdp.handle.RefreshTokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //swagger路径
        String[] excludePatterns = new String[]{"/swagger-resources/**", "/webjars/**", "/v2/**",
                "/swagger-ui.html/**", "/api", "/api-docs", "/api-docs/**", "/doc.html/**"};
        //token刷新拦截器
        registry.addInterceptor(refreshTokenInterceptor()).addPathPatterns("/**");
        //登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(excludePatterns)
                .excludePathPatterns(
                        "/shop/**",
                        "/voucher/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/blog/hot",
                        "/user/code",
                        "/user/login"
                );
    }

    /**
     * 将拦截器入成bean，托管给IOC容器
     * @return
     */
    @Bean
    public RefreshTokenInterceptor refreshTokenInterceptor() {
        return new RefreshTokenInterceptor();
    }
}
